package com.yimi.appstore.anim;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.view.animation.Transformation;

/** 
 * 类说明：   
 * @author  Terry Lu
 * @date    2012-2-1
 * @version 1.0
 */
public class ParabolaAnimation extends Animation {
    private int mFromXType = ABSOLUTE;
    private int mToXType = ABSOLUTE;

    private int mFromYType = ABSOLUTE;
    private int mToYType = ABSOLUTE;
    
    private float mFromXValue = 0.0f;
    private float mToXValue = 0.0f;

    private float mFromYValue = 0.0f;
    private float mToYValue = 0.0f;

    private float mFromXDelta;
    private float mToXDelta;
    private float mFromYDelta;
    private float mToYDelta;
    
    private float mFromDegrees = 0.0f;
    private float mToDegrees = 1080.0f;
    private float mPivotX;
    private float mPivotY;
    
	public ParabolaAnimation(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	public ParabolaAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
		 mFromXValue = fromXDelta;
		 mToXValue = toXDelta;
		 mFromYValue = fromYDelta;
		 mToYValue = toYDelta;
	}
	
	@Override
	public void initialize(int width, int height, int parentWidth,
			int parentHeight) {
		// TODO Auto-generated method stub
		super.initialize(width, height, parentWidth, parentHeight);
	    mFromXDelta = resolveSize(ABSOLUTE, mFromXValue, width, parentWidth);
        mToXDelta = resolveSize(ABSOLUTE, mToXValue, width, parentWidth);
        mFromYDelta = resolveSize(ABSOLUTE, mFromYValue, height, parentHeight);
        mToYDelta = resolveSize(ABSOLUTE, mToYValue, height, parentHeight);
        
        mPivotX = resolveSize(RELATIVE_TO_SELF, 0.5f, width, parentWidth);
        mPivotY = resolveSize(RELATIVE_TO_SELF, 0.5f, height, parentHeight);
        Log.d("", "mFromXDelta:"+mFromXDelta+",mToXDelta:"+mToXDelta+",mFromYDelta:"+mFromYDelta+",mToYDelta:"+mToYDelta);
        Log.d("","mPivotX:"+mPivotX+",mPivotY:"+mPivotY);
	}
	
	@Override
	public boolean getTransformation(long currentTime,
			Transformation outTransformation) {
		// TODO Auto-generated method stub
		final long startOffset = getStartOffset();
		final long duration = getDuration();
		float normalizedTime;
		if (duration != 0) {
			normalizedTime = ((float) (currentTime - (getStartTime() + startOffset))) /
					(float) duration;
		} else {
			// time is a step-change with a zero duration
			normalizedTime = currentTime < getStartTime() ? 0.0f : 1.0f;
		}

		mNormalizedTime = normalizedTime;
	        
		return super.getTransformation(currentTime, outTransformation);
	}
	
	private float mNormalizedTime;
	
	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		// TODO Auto-generated method stub
		float dx = mFromXDelta;
		float dy = mFromYDelta;
		if (mFromXDelta != mToXDelta) {
			dx = mFromXDelta + ((mToXDelta - mFromXDelta) * interpolatedTime);
		}
		if (mFromYDelta != mToYDelta) {
			dy = mFromYDelta + ((mToYDelta - mFromYDelta) * interpolatedTime);
		}
		Log.d("", "dx:"+dx+",dy:"+dy);
		float degrees = mFromDegrees + ((mToDegrees - mFromDegrees) * mNormalizedTime);
		
        t.getMatrix().reset();
        t.getMatrix().postRotate(degrees, mPivotX, mPivotY);
		t.getMatrix().postTranslate(dx, dy);
	}
	
	
}
